import { NextApiRequest, NextApiResponse } from 'next';
import { withIronSessionApiRoute } from 'iron-session/next';
import { ISession } from '@/pages/api/index';
import { ironOptions } from '@/config/index';

const sendVerifyCode = async (req: NextApiRequest, res: NextApiResponse) => {
  const session: ISession = req.session
  const verifyCode = Math.floor(Math.random() * (9999 - 1000)) + 1000;
  const { to = '' } = req.body;

  if (to !== '') {
    session.verifyCode = verifyCode; // 保存验证码到 session 中
    await session.save()

    res.status(200).json({
      code: 200,
      msg: 'success',
      data: {
        code: verifyCode
      }
    })
  } else {
    res.status(200).json({
      code: 0,
      msg: 'error'
    })
  }


}

export default withIronSessionApiRoute(sendVerifyCode, ironOptions)